<!--
 * @描述: 数据权限
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
-->
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.menuName" :placeholder="$t('sysmenu.menuName')" clearable class="filter-input filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.menuPath" :placeholder="$t('sysmenu.menuPath')" clearable class="filter-input filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves type="primary" class="filter-item" icon="el-icon-search" @click="handleFilter">
        {{ $t('common.query') }}
      </el-button>
      <el-button v-waves type="primary" class="filter-item" icon="el-icon-refresh-right" @click="handleReset">
        {{ $t('common.reset') }}
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      row-key="id"
      :data="list.slice((listQuery.currentPage-1)*listQuery.pageSize,listQuery.currentPage*listQuery.pageSize)"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column :label="$t('sysmenu.menuName')" prop="menuName" />
      <el-table-column :label="$t('sysmenu.menuCode')" prop="menuCode" />
      <el-table-column :label="$t('sysmenu.menuIcon')" prop="menuIcon" />
      <el-table-column :label="$t('sysmenu.menuPath')" prop="menuPath" />
      <el-table-column :label="$t('sysmenu.menuSequence')" prop="menuSequence" />
      <el-table-column :label="$t('sysmenu.menuStatus')" class-name="status-col">
        <template slot-scope="{row}">
          <el-tag :type="row.menuStatus | statusFilter">
            <span v-if="row.menuStatus === 1">{{ $t('common.show') }}</span>
            <span v-else>{{ $t('common.hide') }}</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.operate')" align="center">
        <template slot-scope="{row}">
          <el-button type="text" style="margin-left: 10px" @click="handleAuthorize(row.menuCode)">
            <i class="el-icon-connection" />{{ $t('common.authorize') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-drawer :title="$t('sysmenu.dataTitle')" :visible.sync="drawerTableVisible" direction="rtl" size="60%">
      <el-button style="margin-left: 10px" type="primary" icon="el-icon-plus" @click="handleCreate">
        {{ $t('common.add') }}
      </el-button>
      <el-table :data="roleFieldDataSourceList">
        <el-table-column :label="$t('sysrole.roleCode')" prop="roleCode" min-width="30%" />
        <el-table-column :label="$t('sysmenu.dataFilter')" prop="dataField" />
        <el-table-column :label="$t('common.operate')" align="center" min-width="30%">
          <template slot-scope="{row,$index}">
            <el-button type="text" @click="handleUpdate(row)">
              <i class="el-icon-edit-outline" />{{ $t('common.edit') }}
            </el-button>
            <el-popover :ref="'popover-' + row.roleCode" placement="top" width="160" :title="$t('common.confirm-delete')" trigger="click">
              <div>
                <el-button size="mini" @click="$refs[`popover-` + row.roleCode].doClose()">{{ $t('common.cancel') }}</el-button>
                <el-button type="primary" size="mini" @click="$refs[`popover-` + row.roleCode].doClose();handleDelete(row,$index)">{{ $t('common.confirm') }}</el-button>
              </div>
              <el-button slot="reference" type="text" style="margin-left: 10px">
                <i class="el-icon-delete" />{{ $t('common.delete') }}
              </el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>

    <el-dialog v-el-drag-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" :label-position="labelPosition" label-width="210px" style="width: 100%">
        <el-form-item :label="$t('sysrole.roleCode')" prop="roleCode">
          <el-select v-model="temp.roleCode" :disabled="dialogStatus==='update'" :placeholder="$t('common.choose')" clearable style="width: 100%">
            <el-option v-for="item in roleCodeList" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('sysmenu.dataFilter')" prop="dataField">
          <el-input v-model="temp.dataField" :placeholder="$t('common.enter')" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t('common.cancel') }}
        </el-button>
        <el-button type="primary" @click="createData()">
          {{ $t('common.confirm') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { querySysMenu, queryRoleCode, queryDataFieldByMenuCode, authorizeMenuDataToRole, deleteDataField } from '@/api/admin'
import waves from '@/directive/waves' // waves directive
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import qs from 'qs'

export default {
  name: 'DataPermission',
  components: { Pagination },
  directives: { waves, elDragDialog },
  filters: {
    statusFilter(menuStatus) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[menuStatus]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        menuName: undefined,
        menuPath: undefined
      },
      multipleSelection: [],
      drawerTableVisible: false,
      roleCodeList: [],
      roleFieldDataSourceList: [],
      labelPosition: 'right',
      roleFieldDataSourceCount: 0,
      temp: {
        id: undefined
      },
      dialogStatus: '',
      textMap: {
        update: this.$t('common.edit'),
        create: this.$t('common.add')
      },
      rules: {
        roleCode: [{ required: true, validator: this.checkRoleCode, trigger: ['blur', 'change'] }],
        dataField: [{ required: true, validator: this.checkDataField, trigger: ['blur', 'change'] }]
      },
      dialogFormVisible: false
    }
  },
  created() {
    if (window.innerWidth < 700) {
      this.labelPosition = 'top'
    }
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      querySysMenu(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.pagination.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    handleReset() {
      this.listQuery.menuName = ''
      this.listQuery.menuPath = ''
      this.handleFilter()
    },
    handleAuthorize(menuCode) {
      this.menuCode = menuCode
      queryRoleCode().then(response => {
        this.roleCodeList = response.data
      })
      queryDataFieldByMenuCode(menuCode).then(response => {
        this.roleFieldDataSourceList = response.data.list
        this.roleFieldDataSourceCount = response.data.count
      })
      this.drawerTableVisible = true
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    resetTemp() {
      this.temp = {
        id: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.menuCode = this.menuCode
          authorizeMenuDataToRole(qs.stringify(this.temp)).then(response => {
            if (response.message === 'success') {
              this.dialogFormVisible = false
              queryDataFieldByMenuCode(this.menuCode).then(response => {
                this.roleFieldDataSourceList = response.data.list
                this.roleFieldDataSourceCount = response.data.count
              })
            } else {
              this.handleWarning(response)
            }
          })
        }
      })
    },
    handleDelete(row, index) {
      this.temp.roleCode = row.roleCode
      this.temp.menuCode = this.menuCode
      deleteDataField(qs.stringify(this.temp)).then(response => {
        if (response.message === 'success') {
          this.$message({
            message: this.$t('common.delete-success'),
            type: 'success',
            duration: 2000
          })
          this.roleFieldDataSourceList.splice(index, 1)
        } else {
          this.handleWarning(response)
        }
      })
    },
    checkRoleCode(rule, value, callback) {
      if (!value) {
        return callback(new Error(this.$t('sysmenu.roleCode-required')))
      } else {
        callback()
      }
    },
    checkDataField(rule, value, callback) {
      if (!value) {
        return callback(new Error(this.$t('sysmenu.dataField-required')))
      } else {
        callback()
      }
    },
    handleWarning(response) {
      this.$message({
        message: response.message || response,
        type: 'warning',
        duration: 2000
      })
    }
  }
}
</script>
<style>
  .el-drawer__header {
    padding: 10px;
  }
</style>
